package com.yqRecite.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqRecite.entity.Comments;
import com.yqRecite.mapper.CommentsMapper;
import com.yqRecite.service.CommentsService;
import com.yqRecite.util.result.Result;
import com.yqRecite.util.result.ResultUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CommentsServiceImpl implements CommentsService {
    @Resource
    private CommentsMapper commentsMapper;
    /**
     * 增加一条评论
     * @param comments
     * @return
     */
    @Override
    public Result addOneComment(Comments comments) {
        int res = commentsMapper.insert(comments);
        if (res == 1){
            return ResultUtil.ok(comments).setDesc("评论成功");
        }
        return ResultUtil.error("评论时出现异常", comments);
    }

    /**
     * 获取所有评论
     * @return
     */
    @Override
    public Result getAllCommentList() {
        List<Comments> commentsList = commentsMapper.selectList(new QueryWrapper<>());
        if (commentsList != null){
            return ResultUtil.ok(commentsList).setDesc("评论列表获取成功！");
        }
        return ResultUtil.error("评论列表获取失败！", commentsList);
    }

    /**
     * 获取每条化妆品背后的所有评论
     * @param cosmeticsId
     * @return
     */
    @Override
    public Result getAllCommentListByCid(int cosmeticsId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("cosmeticsId", cosmeticsId);
        List<Comments> commentsList = commentsMapper.selectList(wrapper);
        System.out.println("commentList==>"+commentsList);
        if (commentsList != null){
            return ResultUtil.ok(commentsList).setDesc("评论列表获取成功！");
        }
        return ResultUtil.error("评论列表为空！", commentsList);
    }

    /**
     * 删除一条评论
     * @param id
     * @return
     */
    @Override
    public Result deleteComment(int id) {
        int res = commentsMapper.deleteById(id);
        if (res == 1){
            return ResultUtil.ok().setDesc("删除成功！");
        }else {
            return ResultUtil.error("删除失败！");
        }
    }
}
